<?php
declare (strict_types=1);

namespace app\admin\model\finance;

use app\common\model\BaseModel;
use app\school\logic\school\School;
use app\user\logic\user\Account as User;

/**
 * 分成明细模型
 */
class Split extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'user_split';

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     *
     * 时间获取器
     *
     * @TODO 暂不知道为啥没有自动转换
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/4/7
     *
     * @param $value
     * @return false|string
     */
    public function getUpdateTimeAttr($value)
    {
        return date($this->dateFormat, $value);
    }

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        // 如果不是平台时, 加入机构条件限制
        if (!request()->is_platform) {
            $query->where('mhm_id', request()->mhm_id);
        }
    }

    public function userName()
    {
        return $this->belongsTo(User::class)->bind(['user_name', 'superior_user_id']);
    }

    public function superiorUser()
    {
        return $this->belongsTo(User::class, 'superior_user_id')->bind(['superior_user' => 'user_name']);
    }

    public function schoolName()
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_name' => 'title']);
    }
}
